package me.algo;

import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.PriorityQueue;

/**
 * @author guozheng
 * @date 2024/4/12
 */
public class LN {
    public static void main(String[] args) {
        ListNode node = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4,new ListNode(5)))));
        ListNode node1 = reverseKGroup(node,2);

        System.out.println(node1);
    }


    public static ListNode reverseKGroup(ListNode head, int k) {
        if (k == 1 || Objects.isNull(head)) {
            return head;
        }
        ListNode rtn = new ListNode();
        ListNode rcu = rtn;
        ListNode cur = head;
        while (cur != null) {
            int z = k;
            LinkedList<ListNode> nodes = new LinkedList<>();
            while (z > 0 && cur != null) {
                nodes.addLast(cur);
                cur = cur.next;
                z--;
            }
            if (z == 0) {
                while (!nodes.isEmpty()) {
                    rcu.next = nodes.removeLast();
                    rcu = rcu.next;
                    rcu.next = null;
                }
            } else {
                while (!nodes.isEmpty()) {
                    rcu.next = nodes.removeFirst();
                    rcu = rcu.next;
                    rcu.next = null;
                }
            }
        }
        return rtn.next;
    }
    public static ListNode swapPairs(ListNode head) {
        ListNode rtn = head;
        ListNode dummy = new ListNode();
        ListNode dc = dummy;
        ListNode cur = rtn;
        LinkedList<ListNode> stk = new LinkedList<>();
        while (cur != null && cur.next != null) {
            stk.addLast(cur);
            cur = cur.next;
            stk.addLast(cur);
            cur = cur.next;
            ListNode one = stk.removeLast();
            dc.next = one;
            dc = dc.next;
            dc.next = stk.removeLast();
            dc = dc.next;
            dc.next = null;
        }
        if (cur != null) {
            dc.next = cur;
        }
        return dummy.next;
    }


    public static class ListNode {

        int val;
        ListNode next;

        ListNode() {

        }

        ListNode(int val) {

            this.val = val;
        }

        ListNode(int val, ListNode next) {

            this.val = val;
            this.next = next;
        }
    }


    public ListNode mergeKLists(ListNode[] lists) {
        PriorityQueue<ListNode> lns = new PriorityQueue<>(Comparator.comparingInt(o -> o.val));
        for (ListNode list : lists) {
            ListNode cur = list;
            while (cur != null) {
                lns.add(cur);
                cur = cur.next;
            }
        }
        ListNode dummy = new ListNode();
        ListNode cur = dummy;
        while (!lns.isEmpty()) {
            cur.next = lns.remove();
            cur = cur.next;
            cur.next = null;
        }
        return dummy.next;
    }
}
